﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using DictationTool.ViewModel;

namespace DictationTool.View
{
    /// <summary>
    /// Interaction logic for MediaPlayerView.xaml
    /// </summary>
    public partial class MediaPlayerView : UserControl
    {
        public MediaPlayerView()
        {
            InitializeComponent();

            seekTo.MouseLeftButtonUp += HandleSeekToMouseLeftButtonUp;
            seekTo.MouseEnter += new MouseEventHandler(seekTo_MouseEnter);
            seekTo.MouseLeave += new MouseEventHandler(seekTo_MouseLeave);

            seekBtn.MouseLeftButtonDown += new MouseButtonEventHandler(seekBtn_MouseLeftButtonDown);
            seekBtn.MouseMove += new MouseEventHandler(seekBtn_MouseMove);
            seekBtn.MouseLeftButtonUp += new MouseButtonEventHandler(seekBtn_MouseLeftButtonUp);
        }

        private Point startPoint;
        private bool isMoving;
        private void seekBtn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            FrameworkElement element = sender as FrameworkElement;
            element.CaptureMouse();
            element.Cursor = Cursors.Hand;
            startPoint = e.GetPosition(seekTo);
            isMoving = true;
        }

        private void seekBtn_MouseMove(object sender, MouseEventArgs e)
        {
            e.Handled = true;
            if (isMoving)
            {
                Point endPoint = e.GetPosition(seekTo);

                if ((endPoint.X >= 0 && endPoint.X <= mediaPlayerRoot.ActualWidth))
                {
                    var mediaPlayer = this.DataContext as MediaPlayerViewModel;
                    mediaPlayer.HandleSeeking(endPoint.X, mediaPlayerRoot.ActualWidth);
                }
            }
        }

        private void seekBtn_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            FrameworkElement element = sender as FrameworkElement;

            if (isMoving)
            {
                Point endPoint = e.GetPosition(seekTo);

                if ((endPoint.X >= 0 && endPoint.X <= mediaPlayerRoot.ActualWidth))
                {
                    var mediaPlayer = this.DataContext as MediaPlayerViewModel;
                    mediaPlayer.HandleSeekTo(endPoint.X, mediaPlayerRoot.ActualWidth);
                }
            }

            isMoving = false;
            element.ReleaseMouseCapture();
            startPoint.X = 0;
            startPoint.Y = 0;
        }

        private void HandleSeekToMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var mediaPlayer = this.DataContext as MediaPlayerViewModel;
            Point position = e.GetPosition(seekTo);
            mediaPlayer.HandleSeekTo(position.X, mediaPlayerRoot.ActualWidth);
        }

        private void seekTo_MouseLeave(object sender, MouseEventArgs e)
        {
            seekBtn.Visibility = Visibility.Collapsed;
        }

        private void seekTo_MouseEnter(object sender, MouseEventArgs e)
        {
            seekBtn.Visibility = Visibility.Visible;
        }

        private void Grid_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] droppedFilePaths = e.Data.GetData(DataFormats.FileDrop, true) as string[];

                foreach (string droppedFilePath in droppedFilePaths)
                {
                    string file = System.IO.Path.GetFullPath(droppedFilePath);
                    var mediaPlayer = this.DataContext as MediaPlayerViewModel;
                    mediaPlayer.LoadMedia(file);
                }
            }

        }
    }
}
